package ide.modelo;

public class Analizador {

	public boolean analizarHexa(String entrada) {
		boolean salida = true;
		Conversor conversor = new Conversor();
		String[] partes = entrada.split(" +");
		if (partes[0].length() != 4) {
			salida = false;
		} else {

			for (int i = 0; i < 4; i++) {
				if (!conversor.esHexa(partes[0].substring(i, i+1))) {
					salida = false;
				}
			}
		}

		if (salida != false) {

			switch (partes[0].charAt(0)) {
			case '4':
				if (partes[0].charAt(1) != '0') {
					salida = false;
				}
				break;
			case 'A':
				if (partes[0].charAt(2) != '0') {
					salida = false;
				}
				break;
			case 'C':
				if (partes[0].charAt(1) != '0' || partes[0].charAt(2) != '0'
						|| partes[0].charAt(3) != '0') {
					salida = false;
				}
				break;
			case 'D':
			case 'E':
			case 'F':
				salida = false;
				break;
			}
		}

		return salida;
	}

	public boolean analizarEnsam(String entrada) {
		boolean salida = true;
		Conversor conversor = new Conversor();

		String[] partes = entrada.split(" +");

		if (partes.length < 2 && !partes[0].equals("parar")) {
			salida = false;
		} else {

			String instruccion = partes[0];

			if (!conversor.esInstruccion(instruccion)) {
				salida = false;
			} else {

				switch (instruccion){
				case "carmem":
				case "carinm":
				case "rot":
					if(partes[1].split(",").length!=2){
						salida=false;
					} else {
						if(!conversor.esRegistro(partes[1].split(",")[0])){
							salida=false;
						} else {
							// Reviso el argumento en hexadecimal.
							if(partes[1].split(",")[1].length()!=2){
								salida=false;
							} else {
								for (int m=0;m<2;m++){
									if(!conversor.esHexa(partes[1].split(",")[1].substring(m, m+1))){
										salida=false;
									}
								}
							}
						}
					}
					break;
				case "alm":
				case "sal":
					if(partes[1].split(",").length!=2){
						salida=false;
					} else {
						if(!conversor.esRegistro(partes[1].split(",")[1])){
							salida=false;
						} else {
							// Reviso el argumento en hexadecimal.
							if(partes[1].split(",")[0].length()!=2){
								salida=false;
							} else {
								for (int m=0;m<2;m++){
									if(!conversor.esHexa(partes[1].split(",")[0].substring(m, m+1))){
										salida=false;
									}
								}
							}
						}						
					}
					break;
				case "cop":
					if(partes[1].split(",").length!=2){
						salida=false;
					} else {
						for (int i=0;i<2;i++){
							if(!conversor.esRegistro(partes[1].split(",")[i])){
								salida=false;
							}
						}
					}
					break;
				case "suma2":
				case "sumpf":
				case "or":
				case "and":
				case "xor":
					if(partes[1].split(",").length!=3){
						salida=false;
					} else {
						for (int i=0;i<3;i++){
							if(!conversor.esRegistro(partes[1].split(",")[i])){
								salida=false;
							}
						}
					}
					break;
				case "parar":
					break;
				}
			}
		}
		return salida;
	}
}